package com.mystudy.leetcode.problem.sort.p_973;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @program: infoalgorithm
 * @description: 最接近原点的K个点
 * @author: zhouzhilong
 * @create: 2019-08-02 15:39
 **/
public class Solution {
    private static final Logger LOGGER = LoggerFactory.getLogger(Solution.class);

    long startTime = 0L;
    long endTime = 0L;


    @Before
    public void before() {
        startTime = System.nanoTime();
    }

    @Test
    public void test() {
        int [][] tt = new int[][]{{3,3},{5,-1},{-2,4}};
        int[][] ints = kClosest(tt, 2);
        for (int[] anInt : ints) {
            LOGGER.debug("anInt = [{}]",anInt);
        }
    }

    @After
    public void after() {
        endTime = System.nanoTime();
        double cost = (endTime - startTime) / 1000000.0;
        LOGGER.debug("cost = [{} ms ]",cost);
    }




    public int[][] kClosest(int[][] points, int K) {
        int len = points.length;
        int[][] result = new int[K][];
        int min = Integer.MAX_VALUE;
        int count = 0;
        for (int[] point : points) {
            LOGGER.debug("point = [{}]",point);
            int distance = point[0] * point[0] + point[1] * point[1];
            LOGGER.debug("distance = [{}]",distance);
            if (distance < min) {
                min = distance;
                //清空result
                result = new int[K][];
                count = 0;
                result[count] = point;
                count++;
            } else if (distance == min) {
                if(count ==K){
                    result = new int[K][];
                    count = 0;
                }else{
                    result[count] = point;
                    count++;
                }
            }
        }
        return result;
    }
}

